<link href="templates/original/style.css" rel="stylesheet" type="text/css" />
<link href="../includes/jscript/css/ui.all.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../modules/admin/asset/javascripts/ibox/ibox.js"></script>
<script src="../modules/admin/asset/javascripts/asset.js"></script>
<style type="text/css">#viewlogtable tr td:hover{color:blue;}</style>
<?php
/* ========== configuration */
$rows_per_page = 20; // records on one page
$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'name';       
$sort_order = 'asc'; 

/* ========== functions */
function checkavailable($var1, $var2, $var3) // var1=1: available; var2:asset_id;  var3: checkout string
{
	if($var1 == '1') // available
		return '<input type="button" name="checkout" value="Checkout" title="Checkout this asset" style="width:65px;" onclick="asset('."'checkout','".$var2.'\');" />';
	else
	{	
		$username = explode(",",$var3);
		if(mysql_num_rows(mysql_query("SELECT * FROM tbladmins WHERE id='".$_SESSION['adminid']."' AND username='".$username[0]."';")) > 0)
			return '<input type="button" name="checkin" value="Checkin" title="Checkin this asset" style="width:65px;" onclick="asset('."'checkin','".$var2.'\');" />'; 
		return '<img src="../modules/admin/asset/images/not_available.gif" width="15px" height="15px" border="0" alt="Not available" title="This asset is not available now"/>';
	}
}

function isadmin()
{
	$userrow = mysql_fetch_array(mysql_query("SELECT username FROM tbladmins WHERE id='".$_SESSION['adminid']."';"));			
	if(mysql_num_rows(mysql_query("SELECT * FROM tbladmins WHERE roleid='1' AND username='".$userrow[0]."';")) > 0)
		return 1;
	return 0;
}

function isdelete($id)
{
	$isadmin = isadmin();
	if($isadmin == 1)
		return '&nbsp;&nbsp;&nbsp;<img title="Delete this asset" src="../modules/admin/asset/images/delete.png" width="12px" height="12px" onclick="asset('."'delete','".$id.'\');" />';
}

function showdepartment()
{
   $department = mysql_query("SELECT * FROM mod_config");
   $str = '<select id="assetdepartment>';
   while($row = mysql_fetch_array($department)) 
      $str .= '<option value="'.$row["ID"].'">'.$row["department"].'</option>';
   $str .= '</select>';
   return $str;
}

// Sorting
if(isset($_GET['sortby'])) 
{ 
	if($_GET['sortby'] == 'asc') 
		$sort_order = 'desc'; 
	else  
		$sort_order = 'asc'; 
} 
switch($sort) 
{ 
	case 'name': 
		$order_by = 'name'; 
		break; 
	case 'department': 
		$order_by = 'department'; 
		break; 
	case 'checkout': 
		$order_by = 'checkout'; 
		break; 
 } 

/* ========== basic authentication check */
if (!isset($_SESSION["adminid"]) || !defined('ROOTDIR'))
{
	exit();
}

/* ========== display */
$isadmin = isadmin();
$sqlgetAll = "SELECT * FROM mod_asset ORDER BY $order_by $sort_order;";
$dbgetAll = mysql_query($sqlgetAll);
$department = mysql_query("SELECT * FROM mod_config");

// Tabs
echo '
	<div id="tabs">
		<ul>
 			<li id="tab0" class="tab"><a href="javascript:;">Filter</a></li>
 			<li id="tab1" class="tab"><a href="javascript:;">Insert</a></li>
		</ul>
	</div>';
// Tab filter
echo '
	<div id="tab0box" class="tabbox">
  		<div id="tab_content" style="text-align: left;">
		<form name="searchform" method="POST" action="javascript:filter();"><table width="100%">
			<tr>
				<td align="right" width="25%">Name</td>
				<td width="25%"><input id="searchname" type="text" /></td>
				<td align="right" width="25%">Status</td>
				<td width="25%"><select id="searchstatus"><option value="">Any status</option><option value="1">Available</option><option value="0">Busy</option></select></td>
			</tr>
			<tr>
				<td align="right" >Checkout by</td>
				<td><input id="searchcheckoutby" type="text" /></td>
				<td align="right">Department</td>
				<td><select id="searchdepartment"><option value="">All department</option>';
         while($row = mysql_fetch_array($department)) 
			echo '<option value="'.$row["ID"].'">'.$row["department"].'</option>';    
			echo '</td>				
			</tr>
			<tr>				
				<td colspan="4" align="center"><input type="submit"  value="Filter"  /></td>
			</tr>			
		</table></form>
  		</div>
  	</div>';
// Tab insert
if($isadmin == 1)
{
$department = mysql_query("SELECT * FROM mod_config");
echo '
	<div id="tab1box" class="tabbox">
		<div id="tab_content" style="text-align: left;">
		<div id="insert_response"></div>
		<form action="javascript:assetinsert();" name="insertform" method="POST">
		<table>
         <tr align="right"><td>Department:</td><td align="left"><select id="assetdepartment">';
         while($row = mysql_fetch_array($department)) 
			echo '<option value="'.$row["ID"].'">'.$row["department"].'</option>';   
         echo '</select></td></tr>			
			<tr align="right"><td>Serial Number:</td><td><input type="text" id="assetserialnumber" /></td></tr>
			<tr align="right"><td>Name:</td><td><input type="text" id="assetname" /></td></tr>			
			<tr align="right"><td colspan="2"><input type="submit" name="insert" value="Insert" onclick="return checkinsert();"/></td></tr>
		</table>
		</form>
		</div>
	</div>';
}
if($isadmin == 0)
{
	echo '
	<div id="tab1box" class="tabbox">
		<div id="tab_content" style="text-align: left;">
		<b>Access denied!</b>
		</div>
	</div>';
}
// Divider
echo '<br/>';
echo '<div id="searchcontent">';
// Top paging
$total = mysql_num_rows(mysql_query("SELECT * FROM mod_asset;"));
$page = isset ( $_GET["page"] ) ? intval ( $_GET["page"] ) : 1;
echo "<table width='100%'>
		<tr><td>".$total." records found  |  Page ".$page." of ".ceil($total/$rows_per_page)."</td></tr></table>";
// Bottom paging
$page_start = ($page - 1) * $rows_per_page; 
$page_end = $page * $rows_per_page;
$number_of_page = ceil($total/$rows_per_page); 

if($number_of_page > 1) 
{ 
    $list_page = " <td>Page: </td>"; 
    for( $i = 1; $i <= $number_of_page; $i++ ) 
    { 
        if ( $i == $page ) 
            $list_page .= " <td>[ <b>{$i}</b> ]</td> ";         
        else         
            $list_page .= "<td><a href='?module=asset&page={$i}'> {$i} </a></td>";     
    } 
} 
$i = 0; 

echo '
	<div>
		<table cellspacing="1px" cellpadding="5px" class="assettable" border="0" bgcolor="#CCC" width="100%">
			<tr align="center" bgcolor="#EEE">
                <th>Serial Number</th>
                <th><a href="?module=asset&sort=name&sortby='.$sort_order.'">Name</a></th>
                <th><a href="?module=asset&sort=department&sortby='.$sort_order.'">Department</th>
                <th><a href="?module=asset&sort=checkout&sortby='.$sort_order.'">Checkout</th>
                <th>Action</th><th>History</th>
            </tr>';

while($rowgetAll = mysql_fetch_array($dbgetAll))
{
    if($i >= $page_start)
    {   
        $viewlogid = 'viewlog_'.$rowgetAll["ID"];
        echo '<tr align="left" bgcolor="#FFF" id="'.$rowgetAll["ID"].'">
                <td>'.$rowgetAll["serialnumber"].'</td>
                <td>'.$rowgetAll["name"].'</td>
                <td>'.$rowgetAll["department"].'</td>					
                <td align="center">'.$rowgetAll["checkout"].'</td>		
                <td align="center" width="100px">'
                	.checkavailable($rowgetAll["available"],$rowgetAll["ID"],$rowgetAll["checkout"]). isdelete($rowgetAll["ID"]) .'</td>
                <td align="center"><a href="../modules/admin/asset/functions.php?do=viewlog&id='.$rowgetAll["ID"].'" rel="ibox" title="History">View</a></td>
              </tr>';                                          
    }             
    $i++; 
    if($i >= $page_end) 
        break; 
}		
echo '</table></div><br/><div><table><tr>'.$list_page.'</tr></table></div></div>';
?>
